package com.hunttown.mes.manage.controller._basic;

import com.hunttown.mes.common.utils.DateConvertUtils;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.manage.controller.common.ModelService;
import com.hunttown.mes.manage.service.AnalysisManageManageService;
import com.hunttown.mes.manage.service.TransportOperateLogManageService;
import com.hunttown.mes.manage.service.TransportUserManageService;
import com.hunttown.mes.rpc.domain.AnalysisManageDTO;
import com.hunttown.mes.rpc.domain.TransportOperateLogDTO;
import com.hunttown.mes.rpc.domain.TransportUserDTO;
import com.hunttown.mes.rpc.domain.enums.log.ModelTypeEnum;
import com.hunttown.mes.rpc.domain.enums.log.OperateTypeEnum;
import com.hunttown.mes.rpc.domain.query.TransportOperateLogDTOQuery;
import com.hunttown.common.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import static com.hunttown.mes.common.utils.PageUtils.PagePackage;

/**
 * @title: 操作日志
 * @author: wangjunfu
 * @date: 2019-08-12 09:57:54
 * @description:
 */
@Controller
@RequestMapping(value = "/basic/oplog")
public class OperateLogController {

    private final static Logger logger = LoggerFactory.getLogger(OperateLogController.class);

    private final TransportOperateLogManageService logService;
    private final ModelService modelService;
    private final AnalysisManageManageService manageService;
    private final TransportUserManageService userService;

    @Autowired
    public OperateLogController(TransportOperateLogManageService logService, ModelService modelService, AnalysisManageManageService manageService, TransportUserManageService userService) {
        this.logService = logService;
        this.modelService = modelService;
        this.manageService = manageService;
        this.userService = userService;
    }

    // 首页
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model, TransportOperateLogDTOQuery dtoQuery) {

        String showtitle = ModelTypeEnum.getEnumByKey(dtoQuery.getModelType());
        model.addAttribute("showTitle", showtitle);

        if (StringUtils.isNotBlank(dtoQuery.getOpPeopleCn())) {
            if (dtoQuery.getModelType() == 2) {

            } else if (dtoQuery.getModelType() == 3) {
                //会员
                TransportUserDTO user_dto = userService.getByTrueName(dtoQuery.getOpPeopleCn().trim());
                if (user_dto != null) {
                    dtoQuery.setOpPeople(user_dto.getId());
                }
            } else {
                //管理员 & 长期订车
                AnalysisManageDTO admin_dto = manageService.getByAdminName(dtoQuery.getOpPeopleCn().trim());
                if (admin_dto != null) {
                    dtoQuery.setOpPeople(admin_dto.getId());
                }
            }
        }

        // 日期（初始化 2天）
        if (StringUtils.isBlank(dtoQuery.getDaterange())) {
            dtoQuery.setDaterange(String.format("%s ~ %s", DateConvertUtils.getPastDate(1), DateConvertUtils.getPastDate(0)));
        }

        dtoQuery.setOrderBy("order by id desc");
        Page<TransportOperateLogDTO> dataPage = logService.getForPage(dtoQuery);

        // 实时更新列表信息
        if (dataPage != null && dataPage.getItems().size() > 0) {
            for (TransportOperateLogDTO item : dataPage.getItems()) {
                item.setTableName(modelService.getChineseName(item.getTableId()));
                if (item.getModelType() == 2) {
                    //司机
                } else if (item.getModelType() == 3) {
                    //会员
                    item.setOpPeopleCn(modelService.getUserTrueName(item.getOpPeople()));
                } else {
                    //管理员
                    item.setOpPeopleCn(modelService.getManageName(item.getOpPeople()));
                }

                item.setOpTypeName(OperateTypeEnum.getEnumByKey(item.getOpType()));
                item.setOpContentLen(item.getOpContent() == null ? 0 : item.getOpContent().length());
            }
        }

        model.addAttribute("page", dataPage);
        model.addAttribute("PagePackage", PagePackage(dataPage.getPn(), dataPage.getStart(), dataPage.getPageSize(), dataPage.getTotalPages(), dataPage.getTotalItems()));
        model.addAttribute("dtoQuery", dtoQuery);

        //日期
        if (StringUtils.isNotBlank(dtoQuery.getDaterange())) {
            String[] date = dtoQuery.getDaterange().split("~");
            if (date.length == 2) {
                model.addAttribute("startDate", date[0].trim());
                model.addAttribute("endDate", date[1].trim());
            }
        }

        modelService.getChineseEnglishPage(model);

        return "basic/oplog/index";
    }

    //region 数据查看

    /**
     * 数据查看
     *
     * @param model     model模型
     * @param id        主键id
     * @param tableName 数据库英文表名
     * @return
     */
    @RequestMapping(value = "/show", method = RequestMethod.POST)
    public String show(Model model, Integer id, String tableName) {
        id = (id == null ? 0 : id);

        TransportOperateLogDTO obj = new TransportOperateLogDTO();
        obj.setId(id);

        if (id > 0) {
            obj = logService.getById(id);
        }
        logService.assembleObjInfo(model, tableName, obj);

        return "show/show-template";
    }

    //endregion
}